package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    public PageBean page(Integer page, Integer pageSize,
                         String name, Short gender,
                         LocalDate begin,
                         LocalDate end) {
//        Page<Emp> page1 = Page.of(page, pageSize);
//        Page<Emp> p =  lambdaQuery()
//                .like(StringUtils.isNotBlank(name),Emp::getName,name)
//                .eq(gender != null,Emp::getGender,gender)
//                .between(begin != null && end != null,Emp::getEntrydate,begin,end)
//                .page(page1);
//        //封装Bean
//        return new PageBean(p.getTotal(),p.getRecords());

        Page<Emp> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Emp> wrapper =new LambdaQueryWrapper<Emp>()
                .like(name != null, Emp::getName, name)
                .ge(begin != null, Emp::getEntrydate, begin)
                .le(end != null, Emp::getEntrydate, end)
                .eq(gender != null, Emp::getGender, gender);
        empMapper.selectPage(p,wrapper);
        log.info("total：{}   records:{}",p.getTotal(), p.getRecords());
        return new PageBean(p.getTotal(), p.getRecords());
    }


    @Override
    public void delete(List<Long> ids) {
        empMapper.delete(ids);
    }

    @Override
    public boolean save(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());

        empMapper.add(emp);
        return false;
    }

    @Override
    public Emp getById(Long id) {
        Emp emp = empMapper.select(id);

        return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }
    /*@Override
    public PageBean page(Integer page, Integer pageSize) {

        Integer start = (page-1)*pageSize;
        List<Emp> list = empMapper.page(start,pageSize);
        Long count = empMapper.count();

        return new PageBean(count,list);
    }*/
}
